
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppWorkCover;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.service.AppWorkCoverPointService;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppWorkCoverRepository;
import me.zhengjie.service.AppWorkCoverService;
import me.zhengjie.service.dto.AppWorkCoverDto;
import me.zhengjie.service.dto.AppWorkCoverQueryCriteria;
import me.zhengjie.service.mapstruct.AppWorkCoverMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-18
**/
@Service
@RequiredArgsConstructor
public class AppWorkCoverServiceImpl implements AppWorkCoverService {

    private final AppWorkCoverRepository appWorkCoverRepository;
    private final AppWorkCoverMapper appWorkCoverMapper;
    private final AppWorkCoverPointService appWorkCoverPointService;
    private final UserService userService;

    @Override
    public Map<String,Object> queryAll(AppWorkCoverQueryCriteria criteria, Pageable pageable){

        UserDto user = userService.findById(SecurityUtils.getCurrentUserId());

        Page<AppWorkCover> page = appWorkCoverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        //查询子标记点数量
        Page<AppWorkCoverDto> page2 = page.map(appWorkCoverMapper::toDto);
        page2.getContent().forEach(appWorkCoverDto -> {
            int num = appWorkCoverPointService.selSonNum(appWorkCoverDto.getId(),user.getAreaId());
            appWorkCoverDto.setNum(num);
        });
        return PageUtil.toPage(page2);
    }

    @Override
    public List<AppWorkCoverDto> queryAll(AppWorkCoverQueryCriteria criteria){
        return appWorkCoverMapper.toDto(appWorkCoverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppWorkCoverDto findById(Integer id) {
        AppWorkCover appWorkCover = appWorkCoverRepository.findById(id).orElseGet(AppWorkCover::new);
        ValidationUtil.isNull(appWorkCover.getId(),"AppWorkCover","id",id);
        return appWorkCoverMapper.toDto(appWorkCover);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppWorkCoverDto create(AppWorkCover resources) {
        return appWorkCoverMapper.toDto(appWorkCoverRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppWorkCover resources) {
        AppWorkCover appWorkCover = appWorkCoverRepository.findById(resources.getId()).orElseGet(AppWorkCover::new);
        ValidationUtil.isNull( appWorkCover.getId(),"AppWorkCover","id",resources.getId());
        appWorkCover.copy(resources);
        appWorkCoverRepository.save(appWorkCover);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            appWorkCoverRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AppWorkCoverDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppWorkCoverDto appWorkCover : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", appWorkCover.getTitle());
            map.put("编码", appWorkCover.getCodeno());
            map.put("亮色图标", appWorkCover.getImg());
            map.put("灰色图标", appWorkCover.getImg2());
            map.put("小图标", appWorkCover.getImg3());
            map.put("包含gps点位", appWorkCover.getGps());
            map.put("排序（越小越前）", appWorkCover.getOrdid());
            map.put("状态", appWorkCover.getStatus());
            map.put("创建日期", appWorkCover.getCreateTime());
            map.put("更新日期", appWorkCover.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}